<template>
  <el-dialog title="添加员工 " :visible.sync="dialogFormVisible" top="5vh" width="55%">
    <el-form ref="formInline" :inline="true" :model="formInline" :rules="rules" :label-position="labelPosition"  label-width="100px" size="small">
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属组织:" prop="dept">
            <el-select class="main-select-tree" ref="selectTree" style="width: 130%" clearable
                       v-model="formInline.dept" placeholder="请选择父级菜单">
              <el-option v-for="item in formatData(deptData)" :key="item.value" :label="item.label" :value="item.value"
                         style="display: none;"/>
              <el-tree class="main-select-el-tree" ref="selecteltree"
                       :data="deptData"
                       node-key="id"
                       highlight-current
                       :props="defaultProps"
                       @node-click="handleNodeClick"
                       :current-node-key="this.formInline.dept"
                       :expand-on-click-node="expandOnClickNode"
                       />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联角色:" prop="role">
            <el-select v-model="formInline.role" multiple :style="{width: '130%'}">
            <el-option
              v-for="item in roleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
            </el-select>
           </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="账号:" prop="username">
            <el-input v-model="formInline.username" :style="{width: '140%'}" placeholder="请输入账号/必须唯一"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名:" prop="name">
            <el-input v-model="formInline.name" :style="{width: '140%'}" placeholder="请输入员工姓名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
      <el-form-item label="邮箱:" prop="email">
        <el-input v-model="formInline.email" :style="{width: '140%'}" placeholder="请输入邮箱地址"></el-input>
      </el-form-item>
        </el-col>
      <el-col :span="12">
      <el-form-item label="手机号:" prop="mobile">
        <el-input v-model="formInline.mobile" :style="{width: '140%'}" placeholder="请输入手机号码"></el-input>
      </el-form-item>
        </el-col>
      </el-row>



      <el-row>
        <el-col :span="12">
      <el-form-item label="性别:" prop="gender">
        <el-radio-group v-model="formInline.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="头像图床:" prop="avatar">
            <el-input v-model="formInline.avatar" :style="{width: '140%'}" placeholder="请输入头像url"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注:" prop="description">
        <el-input style='width: 140%' maxlength="125" show-word-limit type="textarea" v-model="formInline.description"></el-input>
      </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('formInline')">确 定</el-button>
      <el-button @click="resetForm('formInline')">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addUserData } from '@/api/user'
  import { getRoleData } from '@/api/role'
  import { successMsg } from '@/utils/tools'
  import { getDeptData } from '@/api/dept'
  export default {
    name: 'Insert',
    created() {
      this.fetchData()
    },
    data() {
      return {
        dialogFormVisible: false,
        labelPosition:'right',
        expandOnClickNode: true,
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        roleOptions:[],
        postOptions:[],
        formInline: {
          dept: '',
          name: '',
          avatar:'',
          role:[],
          username:'',
          email:'',
          mobile:'',
          gender:1,
          description:''
        },
        deptData:[],
        rules:{
          dept:[
            { required: true, message: '请选择所属机构', trigger: 'change' },
          ],
          role:[
            { required: true, message: '请选择角色', trigger: 'change' },
          ],
          username:[
            { required: true, message: '请输入用户账号', trigger: 'blur' },
          ],
          name:[
            { required: true, message: '请输入用户姓名', trigger: 'blur' },
          ],
          avatar:[
            { type: 'url', message: '请输入正确的URL地址', trigger: 'blur' },
          ],
          email:[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
          ],
          mobile:[
            { required: true, message: '请输入手机号码', trigger: 'blur' },
          ],
          description:[
            {
              min: 0, max: 255, message: '长度在 0 到 255 个字符', trigger: 'blur'
            }
          ]
        }
      };
    },
    methods:{
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid){
            addUserData(this.formInline).then(res=>{
              if(res.code===2000) {
                successMsg(res.msg)
                this.$refs[formName].resetFields();
                this.$emit('reloadTable')
                this.dialogFormVisible = false
              }
            })
          }else{
            return false;
          }
        });
      },
      resetForm(formName){
        this.$refs[formName].resetFields();
        this.dialogFormVisible = false
      },
      fetchData:function(){
        getDeptData().then(res=>{
          this.deptData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: true })
        })
        getRoleData({status:'True'}).then(res=>{
          this.roleOptions=res.data.data
        })
      },
      // 四级菜单
      formatData(data){
        let options = [];
        data.forEach((item,key) => {
          options.push({label:item.name,value:item.id});
          if(item.children){
            item.children.forEach((items,keys) => {
              options.push({label:items.name,value:items.id});
              if(items.children){
                items.children.forEach((itemss,keyss) => {
                  options.push({label:itemss.name,value:itemss.id});
                  if(itemss.children){
                    itemss.children.forEach((itemsss,keysss) => {
                      options.push({label:itemsss.name,value:itemsss.id});
                    });
                  }
                });
              }
            });
          }
        });
        return options;
      },
      handleNodeClick(node){
        this.formInline.dept = node.id;
        this.$refs.selectTree.blur();
      }
    }
  }
</script>

<style>
  .main-select-el-tree .el-tree-node .is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .main-select-el-tree .el-tree-node.is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .div_padding{
    padding: 5px 5px;
  }
  .label_padding1{
    padding-left: 50px;
  }
  .label_padding2{
    padding-left: 65px;
  }
  .input_width1{
    width: 150%;
  }
  .input_width2{
    width: 145%;
  }
</style>
